package com.biosan.DaoTnterface;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class JDataSet implements Serializable {

	private String[] columnNames, columnTypeNames;
	
	private List<String[]> table, tableFlag;

	public JDataSet(String[] columnNames, String[] columnTypeNames) {
		super();
		this.table = new ArrayList<String[]>();
		this.columnNames = columnNames;
		this.columnTypeNames = columnTypeNames;
	}

	public JDataSet() {
		super();
		this.table = new ArrayList<String[]>();
	}
	
	public int getRowCount() {
		return table.size();
	}

	public String[] getColumnNames() {
		return columnNames;
	}

	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	}

	public String[] getColumnTypeNames() {
		return columnTypeNames;
	}

	public void setColumnTypeNames(String[] columnTypeNames) {
		this.columnTypeNames = columnTypeNames;
	}

	public List<String[]> getTable() {
		if (table == null)
			table = new ArrayList<String[]>();
		return table;
	}

	public int getColumnCount() {
		if (columnNames != null) { return columnNames.length; } 
		else { if (!table.isEmpty()) { return table.get(0).length; } }
		return 0;
	}

	public String[] getRow(int rowIndex) {
		rowRangeCheck(rowIndex);
		return table.get(rowIndex);
	}

	private void rowRangeCheck(int rowIndex) {
		if (rowIndex < 0 || rowIndex >= table.size()) {
			throw new IllegalArgumentException("rowIndex overflow : " + rowIndex);
		}
	}

	private void columnRangeCheck(int columnIndex) {
		if (columnIndex < 0 || columnIndex >= columnNames.length) {
			throw new IllegalArgumentException("columnIndex overflow : ????" + columnIndex);
		}
	}

	public void addRow(String[] row) {
		table.add(row);
	}

	public void addRow(String[] row, String[] flag) {
		table.add(row);
		tableFlag.add(flag);
	}

	public void removeRow(int rowIndex) {
		table.remove(rowIndex);
	}

	public void clearRow(int rowIndex) {
		table.set(rowIndex, null);
	}

	public void removeFullIndex(int rowIndex) {
		table.remove(rowIndex);
		tableFlag.remove(rowIndex);
	}

	public void setCell(int rowIndex, int columnIndex, String value) {
		rowRangeCheck(rowIndex);
		String[] row = table.get(rowIndex);
		columnRangeCheck(columnIndex);
		row[columnIndex] = value;
	}

	public void setCellFlag(int rowIndex, int columnIndex, String value) {
		rowRangeCheck(rowIndex);
		String[] row = tableFlag.get(rowIndex);
		columnRangeCheck(columnIndex);
		row[columnIndex] = value;
	}

	public void setCell(int rowIndex, String columnName, String value) {
		int columnIndex = indexof(columnName.trim());
		if (columnIndex > -1) {
			setCell(rowIndex, columnIndex, value);
		}
	}

	public String getCell(int rowIndex, int columnIndex) {
		rowRangeCheck(rowIndex);
		String[] row = table.get(rowIndex);
		columnRangeCheck(columnIndex);
		return row[columnIndex];
	}

	public String getCellFlag(int rowIndex, int columnIndex) {
		rowRangeCheck(rowIndex);
		String[] row = tableFlag.get(rowIndex);
		columnRangeCheck(columnIndex);
		return row[columnIndex];
	}

	public String getCell(int rowIndex, String columnName) {
		int columnIndex = indexof(columnName.trim());
		if (columnIndex > -1) {
			return getCell(rowIndex, columnIndex);
		}
		return "";
	}

	public String[] getColumn(int columnIndex) {
		columnRangeCheck(columnIndex);
		String[] column = new String[getRowCount()];
		for (int i = 0; i < column.length; i++) {
			column[i] = table.get(i)[columnIndex];
		}
		return column;
	}

	public String[] getColumn(String columnName) {
		int columnIndex = indexof(columnName.trim());
		if (columnIndex > -1) {
			return getColumn(columnIndex);
		}
		return null;
	}

	private int indexof(String columnName) {
		for (int i = 0; i < columnNames.length; i++) {
			if (columnNames[i].trim().equalsIgnoreCase(columnName))
				return i;
		}
		return -1;
	}
	
}
